package com.knowif.service;

import com.knowif.dao.UserOrderFlowMapper;
import com.knowif.dao.UserOrderMapper;
import com.knowif.dao.UserOrderStatusMapper;
import com.knowif.pojo.UserOrder;
import com.knowif.pojo.UserOrderFlow;
import com.knowif.pojo.UserOrderStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
/**
 * 订单状态 ：1.未支付 2.已支付 3.失效订单 4.导师接单 5.已交易 6.取消订单 7.退款订单 8. 待评价
 * */
@Service
public class UserPayOrder_service {
    @Autowired
    private UserOrderMapper userOrderMapper;
    @Autowired
    private UserOrderFlowMapper userOrderFlowMapper;
    @Autowired
    private UserOrderStatusMapper userOrderStatusMapper;

    @Transactional
    public int insertUserOrder(UserOrder userOrder){
       return userOrderMapper.insertSelective(userOrder);
    }

    @Transactional
    public int updateByPrimaryKeySelective(UserOrder userOrder){
        return userOrderMapper.updateByPrimaryKeySelective(userOrder);
    }

    @Transactional
    public void insertOrder(UserOrder userOrder){
        userOrderMapper.insertSelective(userOrder);
        if(userOrder.getId() == null || userOrder.getId() == 0){
            throw new RuntimeException("下单失败");
        }
        UserOrderStatus status = new UserOrderStatus();
        status.setStatus(userOrder.getStatus());
        status.setUserOrderId(userOrder.getId());
        userOrderStatusMapper.insert(status);
    }

    //订单状态 ： 1.未支付 2.已支付 3.失效订单 4.导师接单 5.已交易 6.取消订单 7.退款订单 8. 待评价
    @Transactional
    public void updateOrder(UserOrder userOrder){
        UserOrderStatus userOrderStatus = new UserOrderStatus();
        userOrderStatus.setUserOrderId(userOrder.getId());
        userOrderStatus.setStatus(userOrder.getStatus());
        userOrderStatusMapper.insertSelective(userOrderStatus);
        userOrderMapper.updateByPrimaryKeySelective(userOrder);
    }
    @Transactional
    public void insertUserOrderFlow(UserOrderFlow userOrderFlow){
        userOrderFlowMapper.insertSelective(userOrderFlow);
    }

    @Transactional
    public void updateUserOrderFlow(UserOrderFlow userOrderFlow){

        userOrderFlowMapper.updateByPrimaryKeySelective(userOrderFlow);
        UserOrder userOrder = userOrderMapper.selectByPrimaryKey(userOrderFlow.getUserOrderId());
        userOrder.setStatus(userOrderFlow.getStatus());
        userOrderMapper.updateByPrimaryKeySelective(userOrder);
    }

    @Transactional
    public void updateFastAskOrderFlow(UserOrderFlow userOrderFlow){
        userOrderFlowMapper.updateByPrimaryKeySelective(userOrderFlow);
    }

    public UserOrder selectUserOrderById(int userOrderId){
        if(userOrderId <= 0){
            return null;
        }
        UserOrder userOrder = null;
        try{
            userOrder = userOrderMapper.selectByPrimaryKey(userOrderId);
        }catch (Exception e){
            e.printStackTrace();
        }
        return userOrder;
    }

    public UserOrderFlow selectUserOrderFlowByUserOrderId(int userOrderId){
        if(userOrderId <= 0){
            return null;
        }
        UserOrderFlow userOrderFlow = null;
        try{
            userOrderFlow = userOrderFlowMapper.selectByUserOrderId(userOrderId);
        }catch (Exception e){
            e.printStackTrace();
        }
        return userOrderFlow;
    }

    public UserOrderFlow selectUserOrderFlowByWechatTradeNo(String wechatTradeNo){
        if(wechatTradeNo == null || wechatTradeNo.isEmpty()){
            return null;
        }
        UserOrderFlow userOrderFlow = null;
        try{
            userOrderFlow = userOrderFlowMapper.selectByWechatTradeNo(wechatTradeNo);
        }catch (Exception e){
            e.printStackTrace();
        }
        return userOrderFlow;
    }

    @Transactional
    public void insertUserOrderStatus(UserOrderStatus status){
        userOrderStatusMapper.insertSelective(status);
    }

    public List<UserOrderStatus> getUserOrderHaving(){
        return userOrderStatusMapper.selectHavingOrderId();
    }

    public List<UserOrder> getUserOrderByStatus(int status){
        return userOrderMapper.selectByStatus(status);
    }

    public UserOrderStatus getUserOrderStatusByOrderId(int userOrderId,int status){
        return userOrderStatusMapper.selectByOrderId(userOrderId,status);
    }

    public String selectToUserOpenidByOrderId(int id){
        return userOrderMapper.selectToUserOpenidByOrderId(id);
    }

    public UserOrderFlow selectUserOrderFlowByFastAskOrderId(Integer fastAskOrderId) {
        return userOrderFlowMapper.selectByFastAskOrderId(fastAskOrderId);
    }
}
